﻿using Abide.Classes;
using Abide_AddOn_API.Classes;
using Abide_AddOn_API.Interfaces;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Security.Permissions;
using System.Windows.Forms;

namespace Abide.Forms
{
    public partial class SettingsForm : Form
    {
        ISettingsPage[] SettingsPages
        {
            get;
            set;
        }

        Form parent = null;

        public SettingsForm(Form Parent)
        {
            this.parent = Parent;
            InitializeComponent();
            LoadSettingsPages();
        }
        private void LoadSettingsPages()
        {
            //Prepare...
            List<ISettingsPage> SettingsPages = new List<ISettingsPage>();
            TabPage tabPage = null;

            foreach (var Item in AbideAddOnManager.Collections) //Iterate through all of the found AddOns
                if (Item.Value != null && !string.IsNullOrEmpty(Item.Value.SettingsPage.Key))   //Check to see if the type exists...
                {
                    //Load
                    ISettingsPage page = Item.Value.CreateSettingsPage();

                    //Check
                    if (page != null)
                    {
                        //Add
                        SettingsPages.Add(page);
                        
                        //Prepare TabPage
                        tabPage = new TabPage(page.Text);

                        //Tab Icon... ?
                        if (page.Icon != null)
                        {
                            tabImageList.Images.Add(page.Icon); //Add Image to list...
                            tabPage.ImageIndex = (tabImageList.Images.Count - 1);   //Set Index...
                        }

                        //Set up...
                        Control Interface = page.Interface;
                        Interface.Dock = DockStyle.Fill;
                        tabPage.Controls.Add(Interface);
                        
                        //Add...
                        settingsTabContainer.TabPages.Add(tabPage);
                    }
                }

            //Set
            this.SettingsPages = SettingsPages.ToArray();
        }
        private void ColorPanelPicker_Click(object sender, EventArgs e)
        {
            Color SelectedColor = ((Panel)sender).BackColor;

            using (ColorDialog ClrDlg = new ColorDialog())
            {
                ClrDlg.Color = SelectedColor;

                if (ClrDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    SelectedColor = ClrDlg.Color;
                    ((Panel)sender).BackColor = ClrDlg.Color;
                }
            }
        }
        private void OptionsForm_Load(object sender, EventArgs e)
        {
            //Initialize...

            //
            //UI
            //
            ThemeComboBox.SelectedItem = Settings.Theme;
            splashScreenCheckBox.Checked = Properties.Settings.Default.ShowSplash;

            //
            //Visuals
            //
            ShowTVIconsCheckBox.Checked = Settings.UseTagViewIcons;

            //
            //Paths
            //
            H2PluginsBox.Text = Settings.H2_PluginsPath;
            H2MainMenuBox.Text = Settings.H2_MainMenuPath;
            H2SharedBox.Text = Settings.H2_SharedPath;
            H2SpSharedBox.Text = Settings.H2_SinglePlayerSharedPath;
            H2BPluginsBox.Text = Settings.H2B_PluginsPath;
            H2BMainMenuBox.Text = Settings.H2B_MainMenuPath;
            H2BSharedBox.Text = Settings.H2B_SharedPath;

            //
            //Addons
            //
            foreach (var page in SettingsPages)
                page.LoadSettings();
        }
        private void ApplyButton_Click(object sender, EventArgs e)
        {
            //Check for Restart Changes
            bool RestartRequired = (Settings.Theme != ThemeComboBox.SelectedItem.ToString());

            //
            //UI
            //
            Settings.Theme = ThemeComboBox.SelectedItem.ToString();
            Properties.Settings.Default.ShowSplash = splashScreenCheckBox.Checked;
            
            //
            //Visuals
            //
            Settings.UseTagViewIcons = ShowTVIconsCheckBox.Checked;

            //
            //Paths
            //
            Settings.H2_PluginsPath = H2PluginsBox.Text;
            Settings.H2_MainMenuPath = H2MainMenuBox.Text;
            Settings.H2_SharedPath = H2SharedBox.Text;
            Settings.H2_SinglePlayerSharedPath = H2SpSharedBox.Text;
            Settings.H2B_PluginsPath = H2BPluginsBox.Text;
            Settings.H2B_MainMenuPath = H2BMainMenuBox.Text;
            Settings.H2B_SharedPath = H2BSharedBox.Text;

            //
            //Addons
            //
            foreach (var page in SettingsPages)
                page.SaveSettings();

            //Reload...
            parent.Refresh();

            //Save
            Properties.Settings.Default.Save();

            //Show Message
            if (RestartRequired)
            {
                DialogResult DlgRes = MessageBox.Show(
                    "To fully apply these settings the application must be restarted. Would you like to restart now?",
                    "Restart?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (DlgRes == System.Windows.Forms.DialogResult.Yes)
                    Application.Restart();
            }
        }
        private void RestoreButton_Click(object sender, EventArgs e)
        {
            //Warn...
            if (MessageBox.Show("Warning: Doing this will reset all of Abide's settings.\r\nAre you sure you would like to continue?", 
                "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                //Reset...
                Settings.CreateAbideValues(true);
            }

            //Re-Initialize...
            OptionsForm_Load(this, e);
        }
        private void OpenThemesButton_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Program.ThemesPath);
        }
        private void H2PluginPathBroswseButton_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog FldDlg = new FolderBrowserDialog())
            {
                FldDlg.Description = "Browse to Halo 2 Plugins Path.";
                if (System.IO.Directory.Exists(Settings.H2_PluginsPath))
                {
                    FldDlg.SelectedPath = Settings.H2_PluginsPath;
                }

                if (FldDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2PluginsBox.Text = FldDlg.SelectedPath;
                }
            }
        }
        private void H2MainMenuBrowseButton_Click(object sender, EventArgs e)
        {
            using (FileDialog OpenDlg = new OpenFileDialog())
            {
                OpenDlg.Title = "Select Mainmenu Map";
                OpenDlg.Filter = "Mainmenu Maps (mainmenu.map)|mainmenu.map";

                if (OpenDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2MainMenuBox.Text = OpenDlg.FileName;

                    //Check for more...
                    string possibleSharedPath = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "shared.map");
                    string possibleSpSharedPath = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "single_player_shared.map");
                    bool sharedFound = System.IO.File.Exists(possibleSharedPath);
                    bool spSharedFound = System.IO.File.Exists(possibleSpSharedPath);

                    if (sharedFound && spSharedFound)
                    {
                        if (MessageBox.Show("Shared and Single Player Shared maps were found in the same directory as the Mainmenu. Would you like to use these maps as your map paths too?", 
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2SharedBox.Text = possibleSharedPath;
                            H2SpSharedBox.Text = possibleSpSharedPath;
                        }
                    }
                    else if (spSharedFound)
                    {
                        if (MessageBox.Show("Single Player Shared map was found in the same directory as the Mainmenu. Would you like to use this as your Single Player Shared path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2SpSharedBox.Text = possibleSpSharedPath;
                        }
                    }
                    else if (sharedFound)
                    {
                        if (MessageBox.Show("Shared map was found in the same directory as the Mainmenu. Would you like to use this as your Shared path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2SharedBox.Text = possibleSharedPath;
                        }
                    }
                }
            }
        }
        private void H2SharedBrowseButton_Click(object sender, EventArgs e)
        {
            using (FileDialog OpenDlg = new OpenFileDialog())
            {
                OpenDlg.Title = "Select Shared Map";
                OpenDlg.Filter = "Shared Maps (shared.map)|shared.map";

                if (OpenDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2SharedBox.Text = OpenDlg.FileName;

                    //Check for more...
                    string possibleMainMenu = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "mainmenu.map");
                    string possibleSpSharedPath = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "single_player_shared.map");
                    bool menuFound = System.IO.File.Exists(possibleMainMenu);
                    bool spSharedFound = System.IO.File.Exists(possibleSpSharedPath);

                    if (menuFound && spSharedFound)
                    {
                        if (MessageBox.Show("Mainmenu and Single Player Shared maps were found in the same directory as the Shared map. Would you like to use these maps as your map paths too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2MainMenuBox.Text = possibleMainMenu;
                            H2SpSharedBox.Text = possibleSpSharedPath;
                        }
                    }
                    else if (spSharedFound)
                    {
                        if (MessageBox.Show("Single Player Shared map was found in the same directory as the Shared map. Would you like to use this as your Single Player Shared path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2SpSharedBox.Text = possibleSpSharedPath;
                        }
                    }
                    else if (menuFound)
                    {
                        if (MessageBox.Show("Mainmenu map was found in the same directory as the Shared map. Would you like to use this as your Mainmenu path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2MainMenuBox.Text = possibleMainMenu;
                        }
                    }
                }
            }
        }
        private void H2SPSharedBrowseButton_Click(object sender, EventArgs e)
        {
            using (FileDialog OpenDlg = new OpenFileDialog())
            {
                OpenDlg.Title = "Select SP Shared Map";
                OpenDlg.Filter = "SP Shared Maps (single_player_shared.map)|single_player_shared.map";

                if (OpenDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2SpSharedBox.Text = OpenDlg.FileName;

                    //Check for more...
                    string possibleMainMenu = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "mainmenu.map");
                    string possibleShared = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "shared.map");
                    bool menuFound = System.IO.File.Exists(possibleMainMenu);
                    bool sharedFound = System.IO.File.Exists(possibleShared);

                    if (menuFound && sharedFound)
                    {
                        if (MessageBox.Show("Mainmenu and Shared maps were found in the same directory as the Single Player Shared map. Would you like to use these maps as your map paths too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2MainMenuBox.Text = possibleMainMenu;
                            H2SharedBox.Text = possibleShared;
                        }
                    }
                    else if (sharedFound)
                    {
                        if (MessageBox.Show("Shared map was found in the same directory as the Single Player Shared map. Would you like to use this as your Shared path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2SharedBox.Text = possibleShared;
                        }
                    }
                    else if (menuFound)
                    {
                        if (MessageBox.Show("Mainmenu map was found in the same directory as the Single Player Shared map. Would you like to use this as your Mainmenu path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2MainMenuBox.Text = possibleMainMenu;
                        }
                    }
                }
            }
        }
        private void H2BMainMenuButton_Click(object sender, EventArgs e)
        {
            using (FileDialog OpenDlg = new OpenFileDialog())
            {
                OpenDlg.Title = "Select Mainmenu Map";
                OpenDlg.Filter = "Mainmenu Maps (mainmenu.map)|mainmenu.map";

                if (OpenDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2BMainMenuBox.Text = OpenDlg.FileName;

                    //Check for more...
                    string possibleShared = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "shared.map");
                    bool sharedFound = System.IO.File.Exists(possibleShared);

                    if (sharedFound)
                    {
                        if (MessageBox.Show("The Shared map was found in the same directory as the Mainmenu map. Would you like to use this as your Shared path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2BSharedBox.Text = possibleShared;
                        }
                    }
                }
            }
        }
        private void H2BSharedButton_Click(object sender, EventArgs e)
        {
            using (FileDialog OpenDlg = new OpenFileDialog())
            {
                OpenDlg.Title = "Select Shared Map";
                OpenDlg.Filter = "Shared Maps (shared.map)|shared.map";

                if (OpenDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2BSharedBox.Text = OpenDlg.FileName;

                    //Check for more...
                    string possibleMainmenu = System.IO.Path.Combine(new System.IO.FileInfo(OpenDlg.FileName).Directory.FullName, "mainmenu.map");
                    bool mainmenuFound = System.IO.File.Exists(possibleMainmenu);

                    if (mainmenuFound)
                    {
                        if (MessageBox.Show("The Mainmenu map was found in the same directory as the Shared map. Would you like to use this as your Mainmenu path too?",
                            "Found more...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            H2BMainMenuBox.Text = possibleMainmenu;
                        }
                    }
                }
            }
        }
        private void H2BPluginBrowseButton_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog FldDlg = new FolderBrowserDialog())
            {
                FldDlg.Description = "Browse to Halo 2 Beta Plugins Path.";
                if (System.IO.Directory.Exists(Settings.H2B_PluginsPath))
                {
                    FldDlg.SelectedPath = Settings.H2B_PluginsPath;
                }

                if (FldDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    H2BPluginsBox.Text = FldDlg.SelectedPath;
                }
            }
        }
        private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Dispose Tab Pages
            foreach (var page in SettingsPages)
                page.Dispose();
        }
        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Apply Settings
            foreach (var page in SettingsPages)
                page.ApplySettings();
        }
    }
}
